package activity.order;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.hutoubenjg.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import activity.driverrepair.DriverRepair;
import activity.login.MainActivity;
import activity.personal.ImageVieMaxActivyt;
import base.BaseActivity;
import base.MyApplcation;
import presenter.SysTemPresenter;
import util.utls.ByAlert;
import util.utls.Const;
import util.utls.IsUtilUid;
import util.utls.TimeFormat;
import util.utls.XImageLoader;
import util.volley.ResultEntity;

/**
 * Created by itboye on 2016/12/20.
 */

public class OrderDeilActivity extends BaseActivity implements Observer {
    String addres, detail, lng, lat, id, data, phone, repairName;

    private TextView tvOrderphone;
    private TextView tvMatter, tvaddress;
    private TextView tvAddreXiangXi;
    private Button btnOrder;
    private TextView tvTitle;
    private TextView tvOrderData;
    private ImageView back;
    RelativeLayout reyoutMap;
    SysTemPresenter presenter;
    ImageView img1, img2, img3;
    LinearLayout img0;
    String img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_detil);
        addres = getIntent().getStringExtra("addres");
        detail = getIntent().getStringExtra("detail");
        lng = getIntent().getStringExtra("lng");
        lat = getIntent().getStringExtra("lat");
        id = getIntent().getStringExtra("id");
        data = getIntent().getStringExtra("data");
        phone = getIntent().getStringExtra("phone");
        img = getIntent().getStringExtra("img");

        repairName = getIntent().getStringExtra("repairName");
        tvMatter.setText(repairName);
        presenter = new SysTemPresenter(this);
        tvOrderphone.setText(phone);
        tvOrderData.setText(TimeFormat.DateFormat(data));
        tvaddress.setText(detail);
        tvAddreXiangXi.setText(addres);

        setPhoness(img);

        tvOrderphone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_DIAL);
                Uri data = Uri.parse("tel:" + phone);
                intent.setData(data);
                startActivity(intent);
            }
        });
        tvTitle.setText("订单详情");
        Log.d("uiduididui", id + IsUtilUid.isUid());


    }

    public void setPhoness(String strs) {
        String str = strs;// 原始字符串
        String[] arrayStr = new String[]{};// 字符数组
        List<String> lists = new ArrayList<String>();// list
        arrayStr = str.split(",");// 字符串转字符数组
        lists = java.util.Arrays.asList(arrayStr);// 字符数组转list
        String[] storeStr = lists.toArray(new String[lists.size()]);// list转成字符数组

        /* 输出字符数组的值 */
        for (final String s : arrayStr) {
            System.out.println(s);


            if (s == null || s.equals("")) {
                img0.setVisibility(View.GONE);

            } else if (storeStr.length <= 0) {

                img0.setVisibility(View.GONE);
            } else if (storeStr.length >= 1) {
                img0.setVisibility(View.VISIBLE);
                View v = LayoutInflater.from(MyApplcation.ctx).inflate(R.layout.layout_items, null);
                ImageView img1 = (ImageView) v.findViewById(R.id.img1);
                XImageLoader.load(Const.IMAGE_HEAD + s, img1);
                ViewGroup.MarginLayoutParams layoutParams = new ViewGroup.MarginLayoutParams(
                        ViewGroup.MarginLayoutParams.WRAP_CONTENT,
                        ViewGroup.MarginLayoutParams.WRAP_CONTENT);
                layoutParams.setMargins(5, 5, 5, 5);
                img1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Intent intent = new Intent(MyApplcation.ctx, ImageVieMaxActivyt.class);
                        intent.putExtra("id", s);
                        startActivity(intent);
                    }
                });
                img0.addView(v, layoutParams);
            }

        }
    }

    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnOrder:
                showProgressDialog("请稍后...", true);
                presenter.onMeetOrder(IsUtilUid.isUid(), id);
                break;
            case R.id.back:
                this.finish();
                break;
            case R.id.reyoutMap:
                Intent intent = new Intent(MyApplcation.ctx, MapActivity.class);
                intent.putExtra("mobile", phone);
                intent.putExtra("addres", addres);
                intent.putExtra("lat", lat);
                intent.putExtra("lng", lng);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            IsUtilUid.tianjia(entity);
            if (entity.getEventType() == presenter.MeetOrder_success) {
                ByAlert.alert(entity.getData() + "");
                finish();
                DriverRepair.onSetFragment().viewPager.setCurrentItem(1);//如果有订单跳转到订单里面
                Intent intent01 = new Intent("weixiu");
                sendBroadcast(intent01);

            }
            if (entity.getEventType() == presenter.MeetOrder_fail) {
                ByAlert.alert(entity.getData() + "");
            }
        }
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
    }
}
